package com.maoye.ossp.biz.brand.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.biz.brand.service.IBrandExcelImportService;
import com.maoye.ossp.common.web.BaseController;

/**
 * 对象功能:品牌资料导入控制器类
 * 
 */
@Controller
@RequestMapping("/biz/brand/excel/")
public class BrandExcelImportController extends BaseController
{
	
	private boolean flag=false; 
	@Resource
	private IBrandExcelImportService excelImportService;
	
	/**
	 * 进入导入界面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("importpage")
	public String importPage(HttpServletRequest request,HttpServletResponse response) throws Exception
	{	
		return "/biz/brand/excel/importExcel";
	}
	
	/**
	 * 模板下载
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping("download")
	public void downloadFile(HttpServletRequest request,
			HttpServletResponse response) throws IOException 
	{

	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("importExcel")
	public String importExcel(MultipartHttpServletRequest request, HttpServletResponse response,Model model,
			RedirectAttributes redirectAttributes) throws IOException
	{
		if(!flag){
			flag =true;
			MultipartFile fileLoad = request.getFile("excelFile");
			String dateType = request.getParameter("dateType");
			List<String> msgList = new ArrayList<String>();
			try 
			{
				String msg = excelImportService.processSheet(fileLoad,msgList,dateType);
				if(null != msg && !"".equals(msg))
				{
					addMessage(model, msg);
				}
			} 
			catch (Throwable e)
			{
				flag =false;
				e.printStackTrace();
			}
			flag =false;
			model.addAttribute("msgList", msgList);
			model.addAttribute("fileName", fileLoad.getOriginalFilename());
		}
		
		return "/biz/brand/excel/importExcel";
	}
	
	@RequestMapping("syn")
	public String synPage(HttpServletRequest request,HttpServletResponse response) throws Exception
	{	
		return "/biz/brand/excel/syn";
	}
}
